﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using System.Web.Routing;
using VQWEB.common;
using VQData;
using System.Data;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for CategoryHandler
    /// </summary>
    public class core_CategoryHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Category
            string add_category = context.Request["add_category"];
            if (!string.IsNullOrEmpty(add_category))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_category>(add_category);
                context.Response.Write(obj.category_id);
            }


            string view_category = context.Request["view_category"];
            if (!string.IsNullOrEmpty(view_category))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }

            string list_category = context.Request["list_category"];
            if (!string.IsNullOrEmpty(list_category))
            {
                RenderList(context);
            }

            string delete_category = context.Request["delete_category"];
            if (!string.IsNullOrEmpty(delete_category))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_category>(delete_category);
                RenderList(context);
            }
            string lockunlock_category = context.Request["lockunlock_category"];
            if (!string.IsNullOrEmpty(lockunlock_category))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_category>(lockunlock_category, "category_active");
                RenderList(context);

            }
            #endregion
        }

        private void RenderSingle(HttpContext context, int Id)
        {
            DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
            object obj = layer.Get<core_category>(
               c => c.category_id.Equals(Id),
               c => new
               {
                   id = c.category_id,
                   type = c.category_type,
                   title = c.category_title,
                   content = c.category_content,
                   active = c.category_active
               }
               );
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {

            DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
            IList<object> list = layer.List<core_category>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.category_id,
                    type = c.category_type,
                    title = c.category_title,
                    content = c.category_content,
                    active = c.category_active
                }
                );

            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
   
}